package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.product.dao.ProductItemCategoryDao;
import com.wing.product.model.entity.ProductItemCategory;
import com.wing.product.model.request.ProductItemCategoryForm;
import com.wing.product.model.response.NodeDataVO;
import com.wing.product.model.response.ProductItemCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品检测项分类-ServiceImpl
 *
 * @author xiayang
 */
@Service
@Slf4j
public class ProductItemCategoryServiceImpl extends ServiceImpl<ProductItemCategoryDao, ProductItemCategory> implements ProductItemCategoryService {

    private static long ROOT_ID = -1l;

    @Override
    public int add(ProductItemCategoryForm productItemCategoryForm) {
        //校验
        ProductItemCategory productItemCategory = new ProductItemCategory();
        CopyOptions copyOptions = CopyOptions.create().setEditable(ProductItemCategory.class).setIgnoreError(true).setIgnoreNullValue(true);
        BeanUtil.copyProperties(productItemCategoryForm, productItemCategory, copyOptions);
        return baseMapper.insert(productItemCategory);
    }

    @Override
    public List<ProductItemCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductItemCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductItemCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductItemCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }


    @Override
    @Transactional
    public int del(Long id) {
        ProductItemCategoryVO productItemCategoryVO = baseMapper.get4appById(id);
        if (productItemCategoryVO != null) {
            int delCount = baseMapper.deleteById(id);
            return delCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int enable(Long id) {
        ProductItemCategoryVO productItemCategoryVO = baseMapper.get4appById(id);
        if (productItemCategoryVO != null) {
            ProductItemCategory productItemCategory = new ProductItemCategory();
            productItemCategory.setId(id);
            productItemCategory.setIsDeleted(true);
            int updateCount = baseMapper.updateById(productItemCategory);
            return updateCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        ProductItemCategoryVO productItemCategoryVO = baseMapper.get4appById(id);
        if (productItemCategoryVO != null) {
            ProductItemCategory productItemCategory = new ProductItemCategory();
            productItemCategory.setId(id);
            productItemCategory.setIsDeleted(false);
            int updateCount = baseMapper.updateById(productItemCategory);
            return updateCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int update(ProductItemCategoryForm productItemCategoryForm) {
        ProductItemCategoryVO productItemCategoryVO = baseMapper.get4appById(productItemCategoryForm.getId());
        if (productItemCategoryVO != null) {
            ProductItemCategory productItemCategory = new ProductItemCategory();
            BeanUtils.copyProperties(productItemCategoryForm, productItemCategory);
            return baseMapper.updateById(productItemCategory);
        }
        return 0;
    }
}
